<?php
error_reporting("E_ALL");ini_set("display_errors", 1);
$getdata=$_REQUEST;
if(empty($getdata))$getdata=$_POST?$_POST:$_GET;
$type=empty($getdata['type'])?'':$getdata['type'];
include("include/function.php");
//header("content-Type: application/json; charset=utf-8");
$config = include("config.php");
include("include/".$config['DB_TYPE'].".class.php");
$db = new db;
$db->connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'], $config['DB_PCONNECT']);
$school = include("data/school.php");
$schoolclass = include("data/schoolclass.php");
        
$page = empty($getdata['page'])?1:$getdata['page'];
$id = empty($getdata['id'])?0:$getdata['id'];
$length=50;
$where="personType=0 and parName='邯郸市复美餐饮配送有限公司'";	
if($id){
    $where.=" and id>".$id;
}
$orderids=[];
$insert=0;
$sql="select * from order_info_hs where ".$where.' order by id limit '.$length;
$list = $db->get_all($sql);if(empty($list))exit('over');
foreach($list as $v){$id=$v['id'];
    if(in_array($id,$orderids))continue;
    $check = $db->get_one("select `student_id`,school_id,class_id from hema_student where `uuid`='".$v['studentUuid']."'");
    if(empty($check['student_id'])){
        $data=[];
        $data['uuid']=$v['studentUuid'];
        $data['name']=$v['studentName'];
        $data['phone']=$v['buyerMobile'];
        $data['school_id']=$data['shop_id']=$data['applet_id']=$data['class_id']=0;
        if(!empty($school[$v['unitid']])){
            $data['school_id']=$school[$v['unitid']]['school_id'];
            $data['shop_id']=$school[$v['unitid']]['shop_id'];
            $data['applet_id']=$school[$v['unitid']]['applet_id'];
            if(!empty($data['school_id']) && !empty($schoolclass[$data['school_id']][$v['className']]))$data['class_id']=$schoolclass[$data['school_id']][$v['className']];
            
        }
        $data['type']=$v['personType'];$v['createtime']=str_replace(' 000','',$v['createtime']);
        $data['create_time']=$data['update_time']=strtotime($v['createtime']);
        $ssql = "insert into hema_student(`".implode('`,`',array_keys($data))."`) values('".implode("','",$data)."');";
        $res = $db->query($ssql);
        if(!$res){
            echo $id;
            exit;
        }
        $insert++;
    }else{
        if(empty($check['school_id'])){
            $data=[];
            if(!empty($school[$v['unitid']])){
                $data['school_id']=$school[$v['unitid']]['school_id'];
                $data['shop_id']=$school[$v['unitid']]['shop_id'];
                $data['applet_id']=$school[$v['unitid']]['applet_id'];
                if(!empty($data['school_id']) && !empty($schoolclass[$data['school_id']][$v['className']]))$data['class_id']=$schoolclass[$data['school_id']][$v['className']];
                
            }
            if($data){
                $sqlarr=[];
                foreach($data as $k1=>$v1){
                    $sqlarr[]="`".$k1."`='".$v1."'";
                }
                $ssql = "update hema_student set ".implode(',',$sqlarr)." where student_id='".$check['student_id']."';";
                $res = $db->query($ssql);
                if(!$res){
                    echo $id;
                    exit;
                }
                $insert++;
            }
        }else if(empty($check['class_id'])){
            $data=[];
            if(!empty($schoolclass[$check['school_id']][$v['className']]))$data['class_id']=$schoolclass[$check['school_id']][$v['className']];
            if($data){
                $sqlarr=[];
                foreach($data as $k1=>$v1){
                    $sqlarr[]="`".$k1."`='".$v1."'";
                }
                $ssql = "update hema_student set ".implode(',',$sqlarr)." where student_id='".$check['student_id']."';";
                $res = $db->query($ssql);
                if(!$res){
                    echo $id;
                    exit;
                }
                $insert++;
            }
        }
    }
}
$log = $id.'导入成功,新增：'.$insert.'条';
echo $log.'<script>location.href="?page='.$page.'&id='.$id.'";</script>';

//echo count($sqlarr)."\n";
//echo implode(";\n",$sqlarr);
?>